home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.10 Oct 93 / Fixed-Point Math / fix.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  2.2 KB  |  82 lines  |  [TEXT/KAHL]

  1. /***************************************
  2. * fix.h
  3. * fixed-point number class interface
  4. ***************************************/
  5.  
  6. #pragma once
  7.  
  8. #include <FixMath.h>
  9. #include <iostream.h>
  10. #include <assert.h>
  11.  
  12. const Fixed fixed_zero = Long2Fix((long) 0);
  13.  
  14. class fix {
  15. private:
  16. // arithmetic operators
  17.     friend inline fix operator+(const fix, const fix);
  18.     friend inline fix operator-(const fix, const fix);
  19.     friend inline fix operator*(const fix, const fix);
  20.     friend inline fix operator/(const fix, const fix);
  21. // I/O operators
  22.     friend ostream& operator<<(ostream&, const fix&);
  23.     friend istream& operator>>(istream&, fix&);
  24. // transcendental functions that are optimized for fixed-point numbers
  25.      friend fix sin(const fix);
  26.     friend fix cos(const fix);
  27. // data value
  28.     Fixed n;
  29. // static data member for computation results
  30.     static fix result;
  31. // private range check member function
  32.     inline void rangecheck();
  33. public:
  34. // constructors
  35.     fix() { n = fixed_zero; };
  36.     fix(const long l) { n = Long2Fix(l); };
  37.     fix(const long double d) { n = X2Fix(d); };
  38. // incremant and decrement operators
  39.     fix operator++()    { n += Long2Fix(1); return *this; }; // prefix
  40.     fix operator--()    { n -= Long2Fix(1); return *this; };
  41.     fix operator++(int) { n += Long2Fix(1); return *this; }; // postfix
  42.     fix operator--(int) { n -= Long2Fix(1); return *this; };
  43. // cast operators
  44.     operator long double();
  45. // other operators...
  46.     fix operator+=(const fix f) { *this = *this + f; return *this; }; 
  47.     fix operator-=(const fix f) { *this = *this - f; return *this; }; 
  48.     fix operator*=(const fix f) { *this = *this * f; return *this; }; 
  49.     fix operator/=(const fix f) { *this = *this / f; return *this; }; 
  50. };
  51.     
  52. inline void fix::rangecheck()
  53. {
  54.     assert((n != 0x7FFFFFFF) && (n != 0x80000000));
  55. }
  56.  
  57. inline fix operator+(const fix a, const fix b)
  58. {
  59.     fix::result.n = a.n + b.n;
  60.     return fix::result;
  61. }
  62.     
  63. inline fix operator-(const fix a, const fix b)
  64. {
  65.     fix::result.n = a.n - b.n;
  66.     return fix::result;
  67. }
  68.     
  69. inline fix operator*(const fix a, const fix b)
  70. {
  71.     fix::result.n = FixMul(a.n,b.n);
  72.     fix::result.rangecheck();
  73.     return fix::result;
  74. }
  75.     
  76. inline fix operator/(const fix a, const fix b)
  77. {
  78.     fix::result.n = FixDiv(a.n,b.n);
  79.     fix::result.rangecheck();
  80.     return fix::result;
  81. }
  82.